package com.data.mv.sql;

import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Stream;

public class SqlUtils {

    public static final int PAGE_SIZE = 100;

    private static final Map<String, List<Map<String, String>>> TABLE_DEFINITION = CollectionUtils.newHashMap(15);

    private static final Map<String, List<String>> TABLE_COLUMN = CollectionUtils.newHashMap(15);

    private static final Map<String, Integer> TABLE_COUNT = CollectionUtils.newHashMap(15);

    private static final String TABLE_PROS[] = {"TABLE_NAME", "COLUMN_NAME", "DATA_TYPE", "DATA_LENGTH", "NULLABLE", "DATA_DEFAULT", "COMMENTS"};

    private static final Set<String> DATA_TYPE = new HashSet<>();

    private static final Map<String, String> ORALE_TO_MYSQL = CollectionUtils.newHashMap(3);

    static {
        ORALE_TO_MYSQL.put("VARCHAR2", "varchar");
        ORALE_TO_MYSQL.put("DATE", "datetime");
        ORALE_TO_MYSQL.put("TIMESTAMP(6)", "datetime");
        ORALE_TO_MYSQL.put("NUMBER", "int");
    }

    public static void addTableDefinition(Map<String, String> e) {
        if (Objects.isNull(TABLE_DEFINITION.get(e.get(TABLE_PROS[0])))) {
            TABLE_DEFINITION.put(e.get(TABLE_PROS[0]), new ArrayList<>());
        }

        Map<String, String> pros = CollectionUtils.newHashMap(3);
        Stream.of(TABLE_PROS).skip(1).forEach(p -> {
            pros.put(p, e.get(p));
        });

        TABLE_DEFINITION.get(e.get(TABLE_PROS[0])).add(pros);
    }

    public static Map<String, List<Map<String, String>>> getTableDefinition() {
        return TABLE_DEFINITION;
    }

    public static void addTableColumn(String key, List<Map<String, String>> cols) {
        if (Objects.isNull(TABLE_COLUMN.get(key))) {
            TABLE_COLUMN.put(key, new ArrayList<>());
        }
        cols.stream().forEach(e -> {
            TABLE_COLUMN.get(key).add(e.get(TABLE_PROS[1]));
        });
    }

    public static Map<String, List<String>> getTableColumn() {
        return TABLE_COLUMN;
    }

    public static void addDataType(Map<String, String> e) {
        DATA_TYPE.add(e.get(TABLE_PROS[2]));
    }

    public static String getMysqlDataType(String oracleType, String length) {
        return Objects.isNull(ORALE_TO_MYSQL.get(oracleType)) ? oracleType : Integer.valueOf(length) > 255 ? "text" : ORALE_TO_MYSQL.get(oracleType);
    }

    public static void setTableCount(String key, int count) {
        TABLE_COUNT.put(key, count);
    }

    public static Map<String, Integer> getTableCount() {
        return TABLE_COUNT;
    }

    public static List<Integer> getPages(String key) {
        if (Objects.isNull(TABLE_COUNT.get(key))) {
            return null;
        }
        List<Integer> pages = new ArrayList<>();
        int size = TABLE_COUNT.get(key) % PAGE_SIZE == 0 ? TABLE_COUNT.get(key) == 0 ? 1 : TABLE_COUNT.get(key) / PAGE_SIZE : TABLE_COUNT.get(key) / PAGE_SIZE + 1;
        for (int i = 1; i <= size; i++) {
            pages.add(i);
        }
        return pages;
    }

    public static boolean checkOver(String key, Integer count) {
        return TABLE_COUNT.get(key).equals(count);
    }
}
